package cn.com.duiba.millionaire.center.biz.service.impl;

import cn.com.duiba.millionaire.center.api.constant.RedisKeyFactory;
import cn.com.duiba.millionaire.center.api.constant.ReturnErrorCode;
import cn.com.duiba.millionaire.center.api.dto.MillionaireCenterResponseDto;
import cn.com.duiba.millionaire.center.api.dto.MillionaireConsumerDto;
import cn.com.duiba.millionaire.center.api.enums.RenascenceTypeEnum;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireConsumerDao;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireRenascenceRecordDao;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireShareCodeRecordDao;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireShareCodeUserRelationDao;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireRenascenceRecordEntity;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireShareCodeRecordEntity;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireShareCodeUserRelationEntity;
import cn.com.duiba.millionaire.center.biz.service.ShareCodeService;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * Created by sty on 1/20/18.
 */
@Service
public class ShareCodeServiceImpl implements ShareCodeService {

    @Autowired
    private MillionaireShareCodeRecordDao millionaireShareCodeRecordDao;
    @Autowired
    private MillionaireShareCodeUserRelationDao millionaireShareCodeUserRelationDao;
    @Autowired
    private MillionaireRenascenceRecordDao millionaireRenascenceRecordDao;
    @Autowired
    private MillionaireConsumerDao millionaireConsumerDao;
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, MillionaireConsumerDto> redisTemplate;
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, String> redisTemplateShareCode;

    private String buildConsumerShareCodeKey(Long consumerId, String shareCode, Long appId) {
        return RedisKeyFactory.K013.toString() + consumerId + "_" + shareCode +"_"+appId;
    }


    @Override
    public MillionaireCenterResponseDto inviteUser(String shareCode, Long invitedConsumerId, Long appId) {
        MillionaireCenterResponseDto result = new MillionaireCenterResponseDto();
        if(StringUtils.isNotBlank(shareCode) && appId!=null) {
            shareCode = shareCode.toLowerCase();
        }else{
            result.setSuccess(false);
            result.setErrorCode(ReturnErrorCode.NO_SHARECODE);
            return result;
        }
        //先查询是否有这个分享码
        MillionaireShareCodeUserRelationEntity scUserRelation = millionaireShareCodeUserRelationDao.findByShareCode(shareCode, appId);
        if(scUserRelation == null ){
            result.setSuccess(false);
            result.setErrorCode(ReturnErrorCode.NO_SHARECODE);
            return result;
        }
        if(Objects.equals(scUserRelation.getConsumerId(), invitedConsumerId)){
            result.setSuccess(false);
            result.setErrorCode(ReturnErrorCode.CAN_NOT_INVITE_SELF);
            return result;
        }
        if(redisTemplateShareCode.opsForValue().get(buildConsumerShareCodeKey(invitedConsumerId, shareCode, appId))!=null){
            result.setSuccess(false);
            result.setErrorCode(ReturnErrorCode.ALREADY_USERD);
            return result;
        }
        if( millionaireShareCodeRecordDao.findByShareCodeAndCidAndAppId(shareCode, invitedConsumerId, appId)!=null){
           result.setSuccess(false);
           result.setErrorCode(ReturnErrorCode.ALREADY_USERD);
           return result;
        }

        MillionaireShareCodeRecordEntity shareCodeRecordEntity = new MillionaireShareCodeRecordEntity();
        shareCodeRecordEntity.setInvitedConsumerId(invitedConsumerId);
        shareCodeRecordEntity.setShareCode(shareCode);
        shareCodeRecordEntity.setAppId(appId);
        millionaireShareCodeRecordDao.insert(shareCodeRecordEntity);

        //给双方都加上复活卡
        MillionaireRenascenceRecordEntity reRecordBeInvited= new MillionaireRenascenceRecordEntity();
        reRecordBeInvited.setCount(1);
        reRecordBeInvited.setBizType(RenascenceTypeEnum.BEINVITED.getCode());
        reRecordBeInvited.setBizNum(shareCode);
        reRecordBeInvited.setConsumerId(scUserRelation.getConsumerId());
        millionaireRenascenceRecordDao.insert(reRecordBeInvited);
        millionaireConsumerDao.updateRenascenceNum(scUserRelation.getConsumerId(), 1);

        MillionaireRenascenceRecordEntity reRecordInvite= new MillionaireRenascenceRecordEntity();
        reRecordInvite.setCount(1);
        reRecordInvite.setBizType(RenascenceTypeEnum.INVITEOTHER.getCode());
        reRecordInvite.setBizNum(shareCode);
        reRecordInvite.setConsumerId(invitedConsumerId);
        millionaireRenascenceRecordDao.insert(reRecordInvite);
        millionaireConsumerDao.updateRenascenceNum(invitedConsumerId, 1);

        redisTemplate.delete(Lists.newArrayList(RedisKeyFactory.K008.toString()+scUserRelation.getConsumerId(), RedisKeyFactory.K008.toString()+invitedConsumerId));

        redisTemplateShareCode.opsForValue().set(buildConsumerShareCodeKey(invitedConsumerId, shareCode, appId),"1",5, TimeUnit.MINUTES);
        result.setSuccess(true);
        return result;
    }
}
